package android.eventfinder;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("rawtypes")
public class VenueItermizedOverlay extends ItemizedOverlay {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;
	
	
	public VenueItermizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}
	
	public VenueItermizedOverlay(Drawable defaultMarker, Context context) {
//		  super(defaultMarker);
		 super(boundCenterBottom(defaultMarker));
		  mContext = context;
	}
	
	@Override
	protected boolean onTap(int index) {
	  OverlayItem item = mOverlays.get(index);
	  String information = item.getSnippet();
	  final String[] events = information.split(";");
	  
	  //Use Custom Dialog to show Event Info
	  if(mContext != null){
		  LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		  View view = inflater.inflate(R.layout.dialog,null);
		  LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout_root); 
		  
		  for(int i=0;i<events.length;i++){
			  TextView tv = new  TextView(mContext);  
			  int j = i+1;
			  tv.setText(j+". "+events[i]);   
			  tv.setTextSize(20);
			  layout.addView(tv);    	  
		  }			

		  AlertDialog.Builder dialog = new  AlertDialog.Builder(mContext); 
		  dialog.setTitle(item.getTitle());   
		  dialog.setNegativeButton("Ok", null);
		  dialog.setView(view); 		  
		  dialog.show();
	  }	  
//    Use AlertDialog, ItemList to show info	  
//	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
//	  dialog.setTitle(item.getTitle());
//	  String information = item.getSnippet();
//	  final String[] events = information.split(";");
//	  dialog.setMessage(information);
//	  dialog.setItems(events, null);
//	  dialog.setItems(events, new DialogInterface.OnClickListener() {
//		    public void onClick(DialogInterface di, int item) {
//		  	  AlertDialog.Builder dialog2 = new AlertDialog.Builder(mContext);
//		  	  dialog2.setMessage(events[item]);
//		    }
//		});
//	  dialog.setNegativeButton("Ok", null);

	  return true;
	}

}
